Ext.define('App.commons.report.Report', {
    extend: 'Ext.panel.Panel',
    alias: ['widget.Report'],
    layout: 'vbox',
    items: [],
    scrollable: true,
    defaults: {margin: '0 0 30 0'},
    projectId: null,//外部构建时传入
    reportConfigurationId: null,//外部构建时传入
    reportConfiguration: null,//由loadReportConfiguration加载后初始化
    targetConfiguration: null,//由loadReportConfiguration加载后初始化
    loadReportConfiguration: function () {
        var me = this;
        var loadReportData = function () {
            Ext.Ajax.request({
                url: "report/query_iteration_report",
                params: {
                    'projectId': me.projectId,
                    'reportConfigurationId': me.reportConfigurationId
                },
                success: function (resp) {
                    me.initView([resp.result.data])
                },
                callback: function () {
                    me.unmask();
                }
            });
        }

        Ext.defer(function () {
            me.mask('加载...');
            Ext.Ajax.request({
                url: 'report_configuration/get_configuration_and_target',
                params: {
                    projectId: me.projectId,
                    reportConfigurationId: me.reportConfigurationId
                },
                success: function (resp) {
                    me.reportConfiguration = resp.result.data.reportConfiguration;
                    me.targetConfiguration = resp.result.data.targetConfiguration;
                    loadReportData();
                },
                failure: function () {
                    me.unmask();
                }
            });
        }, 50);
    },
    listeners: {
        afterrender: function () {
            var me = this;
            me.loadReportConfiguration();
        }
    },
    initView: function (datas) {
        var me = this;
        if (!Ext.isEmpty(datas) && datas.length > 0) {
            for (var i = 0; i < datas.length; i++) {
                var report = datas[i];
                if (report.success != true) {
                    me.add(Ext.create('Ext.panel.Panel', {html: report.result}))
                    me.unmask();
                    continue;
                }
                var reportConfiguration = me.reportConfiguration;
                var chartConfigurations = reportConfiguration.chartConfigurations;
                if (!Ext.isEmpty(reportConfiguration)) {
                    var reportDatas = this.initReportData(reportConfiguration.kpis, report.data);
                    //创建表格
                    var grid = Ext.create('App.commons.report.KpiGrid', {
                        header: {height: 28, padding: '5 5 5 5'},
                        title: '隐藏数据',
                        reportConfiguration: reportConfiguration,
                        reportDatas: reportDatas,
                        targetConfiguration: me.targetConfiguration,
                        collapsible: true,
                        collapsed: chartConfigurations.length > 0,
                        collapseMode: 'header', titleCollapse: true,
                        userCls: ['white-header-panel', 'header_cursor_pointer', 'high-line35'],
                        titleAlign: 'right', animCollapse: 50,
                        listeners: {
                            collapse: function () {
                                this.getHeader().setTitle("显示数据")
                            },
                            expand: function () {
                                this.getHeader().setTitle("隐藏数据")
                            }
                        }
                    });//this.createGrid(reportConfiguration, reportDatas);
                    this.add(grid);


                    //创建图
                    var chartBox = this.add(Ext.create({
                        xtype: 'panel', layout: 'column', width: '100%',
                        defaults: {
                            margin: 5,
                            columnWidth: .5,
                            labelWidth: 0
                        }
                    }));
                    this.add(chartBox);
                    for (var j = 0; j < chartConfigurations.length; j++) {
                        var chartConfiguration = chartConfigurations[j];
                        if (chartConfiguration.pie == true) {
                            var chart = Ext.create('App.commons.report.ExtendPolarChart', {
                                chartConfiguration: chartConfiguration,
                                reportDatas: reportDatas,
                                kpis: reportConfiguration.kpis, height: 400, columnWidth: 1, region: 'center'

                            });
                            chartBox.add(chart);
                        } else {
                            var clazz =  'App.commons.echarts.XYChart';
                            var chart = Ext.create(clazz, {
                                chartConfiguration: chartConfiguration,
                                reportDatas: reportDatas,
                                kpis: reportConfiguration.kpis, height: 400, columnWidth: 1
                            });
                            chartBox.add(chart);
                        }
                        // var chart = this.createChart(grid.getStore(), chartConfiguration, reportConfiguration.kpis)
                        //chartBox.add(chart);
                    }
                }
            }
        }
    },
    initReportData: function (kpis, datas) {
        var dts = new Ext.util.MixedCollection();
        Ext.Array.each(kpis, function (kpi) {
            dts.add(kpi.kpiConfiguration.field, kpi.kpiConfiguration.dataType);
        });
        var initedDatas = [];
        Ext.Array.each(datas, function (data) {
            var line = {};
            for (key in data) {
                var value = data[key];
                var dt = dts.get(key);
                if (Ext.isEmpty(value)) {
                    line[key] = null;
                }
                if (Ext.isEmpty(dt) || dt == 'string') {
                    line[key] = value;
                } else if (dt == 'int') {
                    line[key] = parseInt(value)
                } else if (dt == 'float') {
                    line[key] = parseFloat(value)
                } else if (dt == 'date') {
                    line[key] = new Date(value)
                }
            }
            initedDatas.push(line);
        })
        return initedDatas;
    }
})


Ext.define('Ext.chart.theme.Shine', {
    extend: 'Ext.chart.theme.Base',
    alias: 'chart.theme.shine',
    config: {
        colors: ["#e6b600", "#0098d9", "#c12e34", "#2b821d", "#005eaa", "#339ca8", "#cda819", "#32a487"]
    }
});


Ext.define('Ext.chart.theme.CustomTheme', {
    extend: 'Ext.chart.theme.Base',
    alias: 'chart.theme.customtheme',
    config: {
        colors: ["#c23531", "#2f4554", "#61a0a8", "#d48265", "#91c7ae", "#749f83", "#ca8622", "#bda29a", "#6e7074", "#546570", "#c4ccd3"]
    }
});
